package it.slkfarm.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

public class Production
{
	/**
	 * @uml.property  name="cropSelection"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="it.slkfarm.bean.Crop"
	 */
	private List<Crop> cropSelection;
	/**
	 * @uml.property  name="total"
	 */
	private int total;
	/**
	 * @uml.property  name="farm"
	 * @uml.associationEnd  
	 */
	private Farm farm;
	
	
	/**
	 * @return
	 * @uml.property  name="farm"
	 */
	public Farm getFarm() {
		return farm;
	}
	
	/**
	 * @param farm
	 * @uml.property  name="farm"
	 */
	public void setFarm(Farm farm) {
		this.farm = farm;
	}



	public Production()
	{
		cropSelection = new ArrayList<Crop>();
		total = 0;
	}
	
	
	
	public List<Crop> getCropSelection() {
		return cropSelection;
	}

	public void setCropSelection(List<Crop> cropSelection) {
		this.cropSelection = cropSelection;
	}

	/**
	 * @param total
	 * @uml.property  name="total"
	 */
	public void setTotal(int total) {
		this.total = total;
	}

	public void removeCrop(Crop c)
	{
		Iterator<Crop> iterator = cropSelection.iterator();
		boolean find = false;
		Crop app = null;
		while (iterator.hasNext() || !find)
		{
			app = iterator.next();
			if (app.getCultivarName().equals(c.getCultivarName()))
			{
				iterator.remove();
				total--;
				find = true;
			}
		}	
	}
	
	public boolean isCropPresent(Crop c)
	{
		Iterator<Crop> iterator = cropSelection.iterator();
		Crop app = null;
		while (iterator.hasNext())
		{
			app = iterator.next();
			if (app.getCultivarName().equals(c.getCultivarName()))
				return true;
		}
		return false;
	}
	
	public void addCrop(Crop c)
	{
		cropSelection.add(c);
		total++;
		Collections.sort(cropSelection, sorter);
	}
	
	private static Comparator<Crop> sorter = new Comparator<Crop>()
	{

		public int compare(Crop arg0, Crop arg1) 
		{
			double a = arg0.getPercentageProduction() - arg1.getPercentageProduction();
			int x = (int) a;
			return x;
		}
		
	};
}
